package com.example.emailservice.Tools;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class SRandom {
    private Integer oneToFour; // 四位随机整数
    private Integer oneToSix;  // 六位随机整数
    private final Date gmt;   // 标记验证码生成时间
    private final Integer overdue = 1; //设置超时时间长度 单位小时
    private Boolean used;

    public SRandom(){
        Random r = new Random(new Date().getTime());
        oneToFour = r.nextInt(9999);
        oneToSix = r.nextInt(999999);
        used = false;
        gmt = new Date();
    }

    public Date getGmt() {return gmt;}

    public Integer getOneToFour() {
        return oneToFour;
    }

    public Integer getOneToSix() {
        return oneToSix;
    }

    public void setUsed(Boolean used){ this.used = used; }

    public Boolean isUsed(){ return this.used; }

    public Boolean isOverdue(){
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.getGmt());
        calendar.add(Calendar.HOUR_OF_DAY,overdue);
        Date end = calendar.getTime(); // 计算当前时间，过期时间

        if(now.after(this.getGmt()) && now.before(end)) // 判断是否过期
            return false; // 未过期
        else
            return true; // 已过期
    }

}
